﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using BusinessLayer.RegInfo;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.RegInfo.Entities;

namespace BackEnd.Controllers
{
    public class MarketingController : Controller
    {
        //
        // GET: /Marketing/
        public ActionResult Index()
        {
            var marketingBL = new MarketingBL();
            var lstMarketing = new List<MarketingFullInfo>();
            lstMarketing.AddRange(marketingBL.GetAllFullInfoMarketings());

            return View("Index", lstMarketing);
        }

        //
        // GET:/Marketing/Create
        [CustomAuthorize(Action = Constants.AdminActions.CreatMarketing)]
        public ActionResult Create()
        {
            var marketingCatBL = new MarketingCategoryBL();
            var lstCat = marketingCatBL.GetAll();
            var lstSubs = new SubscriptionBL().GetAll();
            ViewData["ListMarketingCats"] = lstCat.ToArray();
            ViewData["ListSubscriptions"] = lstSubs.ToArray();
            return View();
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.CreatMarketing)]
        [ValidateInput(false)]
        public ActionResult Create(MarketingModel model)
        {
            if (ModelState.IsValid)
            {
                var marketingBL = new MarketingBL();
                var check = marketingBL.GetFullInfoByCode(model.Code);
                if (check == null)
                {
                    var marketingCat = new MarketingCategoryBL().GetByID(model.MarketingCatID);
                    if (marketingCat != null)
                    {
                        if (marketingCat.Code.ToLower() == Common.Constants.MarketingCategories.DISCOUNT.ToLower())
                        {
                            var subs = new MarketingDiscount();
                            subs.Code = model.Code;
                            subs.Title = model.Title;
                            subs.Status = Common.Constants.Status.ACTIVE;
                            subs.CreatedBy = Session[Common.Constants.SessionKeys.UserInfo].ToString();
                            subs.Discount = model.Discount;
                            subs.FromDate = model.FromDate;
                            subs.ToDate = model.ToDate;
                            subs.LastUpdatedBy = Session[Common.Constants.SessionKeys.UserInfo].ToString();
                            subs.MarketingCatID = model.MarketingCatID;
                            subs.SubscriptionID = model.SubscriptionID;
                            subs.Contents = model.Contents;



                            int result = marketingBL.CreateMarketingDiscount(subs);
                            if (result > 0)
                            {                                
                                return RedirectToAction("Index");
                            }
                            else
                            {
                                ViewData["ErrorMessage"] = "Has an error when executing insert marketing into database";
                            }
                        }
                        else
                        {
                            var subs = new MarketingCoupon();
                            subs.Code = model.Code;
                            subs.Title = model.Title;
                            subs.Status = Common.Constants.Status.ACTIVE;

                            subs.CreatedBy = Session[Common.Constants.SessionKeys.UserInfo].ToString();
                            subs.CouponDiscount = model.Discount;
                            subs.FromDate = model.FromDate;
                            subs.ToDate = model.ToDate;
                            subs.LastUpdatedBy = Session[Common.Constants.SessionKeys.UserInfo].ToString();

                            subs.MarketingCatID = model.MarketingCatID;
                            subs.SubscriptionID = model.SubscriptionID;
                            subs.CouponCode = model.CouponCode;
                            subs.Contents = model.Contents;


                            int result = marketingBL.CreateMarketingCoupon(subs);
                            if (result > 0)
                            {
                                return RedirectToAction("Index");
                            }
                            else
                            {
                                ViewData["ErrorMessage"] = "Has an error when executing insert marketing into database";
                            }
                        }
                    }
                    else
                    {
                        ViewData["ErrorMessage"] = "Marketing Category not found.";
                    }
                }
                else
                {
                    ViewData["ErrorMessage"] = String.Format("This marketing - {0} exists!", model.Code);

                }

            }
            //ReBind
            var marketingCatBL = new MarketingCategoryBL();
            var lstCat = marketingCatBL.GetAll();
            var lstSubs = new SubscriptionBL().GetAll();
            ViewData["ListMarketingCats"] = lstCat.ToArray();
            ViewData["ListSubscriptions"] = lstSubs.ToArray();
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteMarketing)]

        public ActionResult Delete(string id)
        {

            var marketingBL = new MarketingBL();
            if (!String.IsNullOrEmpty(id))
            {
                List<string> lstOfMarketingIDs = new List<string>();
                foreach (string item in id.Split('_'))
                {
                    int temp = -1;
                    Int32.TryParse(item, out temp);
                    if (temp > 0)
                    {
                        lstOfMarketingIDs.Add(item);
                    }
                }
                int result = marketingBL.DeleteMarketingByIds(lstOfMarketingIDs);
                if (result > 0)
                {                    
                    //return Json(1, JsonRequestBehavior.AllowGet);
                    ViewData["ErrorMessage"] = "Delete successful!";
                    return RedirectToAction("Index");
                }

            }
            //return Json(0, JsonRequestBehavior.AllowGet);
            ViewData["ErrorMessage"] = "Cannot delete selected items!";
            return RedirectToAction("Index");


        }

        [CustomAuthorize(Action = Constants.AdminActions.ViewMarketings)]
        public ActionResult Details(int id)
        {
            var marketingBL = new MarketingBL();
            var marketing = marketingBL.GettFullInfoByMarketingID(id);

            if (marketing == null)
            {
                ViewData["ErrorMessage"] = "Current marketing not found!";
            }




            return View("Details", marketing);
        }

        //
        // GET:/Subscription/Edit/id
        //[AcceptVerbs(HttpVerbs.Get)]
        [CustomAuthorize(Action = Constants.AdminActions.UpdateMarketing)]
        public ActionResult Edit(int id)
        {
            var marketingBL = new MarketingBL();
            var marketing = marketingBL.GettFullInfoByMarketingID(id);
            var marketingModel = new MarketingModel();
            if (marketing != null)
            {
                marketingModel.MarketingID = marketing.MarketingID;
                marketingModel.SubscriptionID = marketing.SubscriptionID;
                marketingModel.Code = marketing.Code;
                marketingModel.Title = marketing.Title;
                marketingModel.CouponCode = marketing.CouponCode;
                if (marketing.CategoryCode.ToLower() == Constants.MarketingCategories.DISCOUNT.ToLower())
                {
                    marketingModel.Discount = marketing.Discount;
                }
                else
                {
                    marketingModel.Discount = marketing.CouponDiscount;
                }

                marketingModel.FromDate = marketing.FromDate;
                marketingModel.ToDate = marketing.ToDate;
                marketingModel.MarketingCatID = marketing.MarketingCatID;
                marketingModel.Status = marketing.Status;
                marketingModel.SubscriptionID = marketing.SubscriptionID;
                marketingModel.Contents = marketing.Contents;
                marketingModel.CategoryCode = marketing.CategoryCode;

            }
            else
            {
                ViewData["ErrorMessage"] = "Current marketing not found!";
            }

            var lstStatus = Common.Constants.Status.GetListStatus();
            ViewData["ListStatus"] = lstStatus.ToArray();

            var marketingCatBL = new MarketingCategoryBL();
            var lstCat = marketingCatBL.GetAll();
            var lstSubs = new SubscriptionBL().GetAll();
            ViewData["ListMarketingCats"] = lstCat.ToArray();
            ViewData["ListSubscriptions"] = lstSubs.ToArray();


            return View("Edit", marketingModel);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.UpdateMarketing)]
        [ValidateInput(false)]
        public ActionResult Edit(MarketingModel model)
        {
            if (ModelState.IsValid)
            {
                var marketingBL = new MarketingBL();
                var check = marketingBL.GettFullInfoByMarketingID(model.MarketingID);
                if (check != null)
                {
                    var marketingCat = new MarketingCategoryBL().GetByID(model.MarketingCatID);
                    if (marketingCat != null)
                    {
                        if (marketingCat.Code.ToLower() == Common.Constants.MarketingCategories.DISCOUNT.ToLower())
                        {
                            var subs = new MarketingDiscount();
                            subs.MarketingID = model.MarketingID;
                            subs.Code = model.Code;
                            subs.Title = model.Title;
                            subs.Status = Common.Constants.Status.ACTIVE;

                            subs.Discount = model.Discount;
                            subs.FromDate = model.FromDate;
                            subs.ToDate = model.ToDate;

                            subs.LastUpdatedBy = Session[Constants.SessionKeys.UserInfo].ToString();
                            subs.Status = model.Status;
                            subs.MarketingCatID = model.MarketingCatID;
                            subs.SubscriptionID = model.SubscriptionID;
                            subs.Contents = model.Contents;

                            int result = marketingBL.UpdateMarketingDiscount(subs);
                            if (result > 0)
                            {                                
                                return RedirectToAction("Index");
                            }
                            else
                            {
                                ViewData["ErrorMessage"] = "Has an error when executing insert marketing into database";
                            }
                        }
                        else
                        {
                            var subs = new MarketingCoupon();
                            subs.MarketingID = model.MarketingID;
                            subs.Code = model.Code;
                            subs.Title = model.Title;
                            subs.Status = Common.Constants.Status.ACTIVE;
                            subs.CouponDiscount = model.Discount;
                            subs.FromDate = model.FromDate;
                            subs.ToDate = model.ToDate;
                            subs.Status = model.Status;
                            subs.LastUpdatedBy = Session[Common.Constants.SessionKeys.UserInfo].ToString();
                            subs.MarketingCatID = model.MarketingCatID;
                            subs.SubscriptionID = model.SubscriptionID;
                            subs.CouponCode = model.CouponCode;
                            subs.Contents = model.Contents;



                            int result = marketingBL.UpdateMarketingCoupon(subs);
                            if (result > 0)
                            {
                                return RedirectToAction("Index");
                            }
                            else
                            {
                                ViewData["ErrorMessage"] = "Has an error when executing update marketing into database";
                            }
                        }
                    }
                    else
                    {
                        ViewData["ErrorMessage"] = "Marketing Category not found.";
                    }
                }
                else
                {
                    ViewData["ErrorMessage"] = String.Format("This marketing - {0} not exists!", model.Code);

                }

            }
            var lstStatus = Common.Constants.Status.GetListStatus();
            ViewData["ListStatus"] = lstStatus.ToArray();

            var marketingCatBL = new MarketingCategoryBL();
            var lstCat = marketingCatBL.GetAll();
            var lstSubs = new SubscriptionBL().GetAll();
            ViewData["ListMarketingCats"] = lstCat.ToArray();
            ViewData["ListSubscriptions"] = lstSubs.ToArray();

            return View(model);
        }

    }
}
